/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package definitions;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashSet;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;
import org.pircbotx.hooks.events.PrivateMessageEvent;
import org.pircbotx.hooks.types.GenericMessageEvent;
import org.pircbotx.Colors;
/**
 *
 * @author tangd
 */
public class DefinitionModule extends ListenerAdapter {
    private LinkedHashSet<String> enabledChannels = new LinkedHashSet<String>();
    private DefinitionFetcher definitionFetcher= null;
    public DefinitionModule(Connection c, String table, LinkedHashSet channels) {
        definitionFetcher = new CachingDefinitionFetcher(c, table);
        this.enabledChannels = channels;
    } 
    @Override
    public void onMessage(MessageEvent event) throws Exception {
        if(!enabledChannels.contains(event.getChannel().getName())) {
//            System.out.println("Channel not enabled "+event.getChannel().getName());
            return;
        }
        handleDefinition(event);
    }
    private void handleDefinition(GenericMessageEvent event) {
        //Insert parsing code here and whatever
        String message = event.getMessage().trim();
        if(message.matches("\\?\\?(\\s)+\\S+")) {
            //If in the form ?? word
            System.out.println("?? word");
            String word = message.split("\\s+")[1];
            Definition d = definitionFetcher.getDefinition(word);
            if(d.getDefinition()==null) {
                event.respond(Colors.BOLD+"?? "+Colors.NORMAL+"no definition found");
                return;
            }
            event.respond(d.toString());
        }
        else if(message.matches("!whatis\\s+\\S+")) {
            //!whatis word
            System.out.println("!whatis");
            String word = message.split("\\s+")[1];
            Definition d = definitionFetcher.getDefinition(word);
            if(d.getDefinition()==null) {
                event.respond(Colors.BOLD+"!whatis "+Colors.NORMAL+"no definition found");
                return;
            }
            event.respond(d.toString());
        }
        else if(message.matches("[^\\s \\?]+\\?")) {
            //word?
            System.out.println("word?");
            String word = message.substring(0, message.length()-1);
            Definition d = definitionFetcher.getDefinition(word);
            if(d.getDefinition()!=null) {
                event.respond(d.toString());
            }
        }
        else if (message.matches("!whodef\\s+\\S+")) {
            //!whodef
            System.out.println("!whodef");
            String word = message.split("\\s+")[1];
            Definition d = definitionFetcher.getDefinition(word);
            if(d.getDefinition()==null) {
                event.respond(Colors.BOLD+"!whodef "+Colors.NORMAL+"no such definition");
                return;
            }
            event.respond(d.whoDefString());
            
        }
        else if (message.matches("!def\\s+\\S+\\s+.*")) {
            //!def
            String word = message.split("\\s+")[1];
            String spaced = message.replaceAll("\\s+", " ");
            String def = spaced.substring(spaced.indexOf(" ", 5));
            System.out.println("!def");
            if(true) {
            // TODO Modify to support authentication     
                if(definitionFetcher.setDefinition(word, def, event.getUser().getNick())!=null) {
                    event.respond(Colors.BOLD+"!def "+Colors.NORMAL+word+" as "+"def");
                }
                else {
                    event.respond(Colors.BOLD+"Error: "+Colors.NORMAL+"Could not connect to database");
                }
            }
            else {
                event.respond("Fuck you");
            }
        }
        else if(message.matches("!remdef\\s+\\S+")) {
            //!remdef
            String word = message.split("\\s+")[1];
            if(true) {
                // TODO Modify to support authentication
                int conditionCode = definitionFetcher.removeDefinition(word);
                if(conditionCode>0) {
                    event.respond(Colors.BOLD+"!remdef "+Colors.NORMAL+"definition removed for "+word);
                }
                else {
                    switch(conditionCode) {
                        case 0 :
                            event.respond(Colors.BOLD+"!remdef "+Colors.NORMAL+"definition for "+word+" not found");
                            break;
                        case -1:
                            event.respond(Colors.BOLD+"!remdef "+Colors.NORMAL+" could not connect to database");
                            break;
                    }
                }
            }
        }
    }
}
